var host = window.location.host
var pathname = window.location.pathname

// 根据返回的url 判断是否是小程序执行环境
function jump2good (id, node) {
  var url = window.location.href
  if (url.indexOf('uuid=wechat') > -1) {
    wx.miniProgram.getEnv(function (res) {
      if (res.miniprogram) {
        wx.miniProgram.navigateTo({url: '/pages/home/goodsDetail/goodsDetail?linkId=' + id})
      }
    })
  } else {
    if (pathname.indexOf('/os/') != -1) {
      node.innerHTML = `<iframe id='app_iframe' style='display:none' src="taojiji://goods?goods=${id}&t=${new Date().getTime()}"></iframe>`
    } else {
      if (host == 'a.shan666.com') {
        var goodsurl ='http://mtest.dabanyx.com/tjj_m.php/Tjj/Tjj/request/api_url/goods-info/display_url/Goods-detail/goods_id/' +
          id + '/is_asynchronous/1'
        window.location.href = goodsurl
      } else {
        var goodsurl ='http://wap.taojiji.com/tjj_m.php/Tjj/Tjj/request/api_url/goods-info/display_url/Goods-detail/goods_id/' +
          id +'/is_asynchronous/1'
        window.location.href = goodsurl
      }
    }
  }
}

window.app = {
  init: function (config) {
    console.log(config)
    this.id = config.id || 'app_iframe'
    this.app_name = config.app_name || 'taojiji'
  },
  t: function () {
    var myDate = new Date()
    return myDate.getTime()
  },
  jump: function (type, params) {
    this.app_name = 'taojiji'
    console.log(this)
    params = params || {}
    if ($('#' + this.id).length == 0) {
      $("<iframe style='display:none' id='" + this.id + "'></iframe>").prependTo('body')
    }

    var url = this.app_name + '://' + type
    var i = 0
    for (var key in params) {
      if (i == 0) { url += '?' } else {url += '&'}
      if (typeof (params[key]) === 'object') {url += key + '=' + JSON.stringify(params[key])} 
      else {url += key + '=' + encodeURIComponent(params[key])}
      i++
    }
    if (i == 0) { url += '?t=' + this.t()} else {url += '&t=' + this.t()}
    console.log(url)
    $('#' + this.id).attr('src', url)
  },
  // 验证是否登录
  checkLogin: function () {
    this.jump('checkLogin')
  },
  // 登入
  login: function (params) {
    this.jump('login', params)
  },
  // 登出
  logout: function () {
    $.get(U('Users/appLogout'))
    this.jump('logout')
  }

}

// 未登录
function nologin (params, node) {
  node.innerHTML = `<iframe id='app_iframe' style='display:none' src="taojiji://goods?goods=${params}&t=${new Date().getTime()}"></iframe>`
}

// 预设地址环境
function resetAddress () {
  var hostLink = ''
  var assetsLink = ''
  var type = null
  var env = process.env.NODE_ENV
  // 在上线到正式线上打开注释
  if (env == 'production') {
    hostLink = 'https://activity.shandjj.com/'
    assetsLink = 'https://mstatic.shandjj.com'
    type = 'onLine'
  } else {
    hostLink = 'http://activity.sdjj.dev/'
    assetsLink = 'http://mstatic.sdjj.dev'
    type = 'devLine'
  };
  return {hostLink,assetsLink,type}
}

export { jump2good, nologin, resetAddress}
// 处理图片路径问题
export function filterPic (e,directory) {
  let PicList = {}
  let baseUrl = window.location.hostname === 'activity.shandjj.com' ? `https://mstatic.shandjj.com/default/${directory}/` : `http://mstatic.sdjjia.com/default/${directory}/`;
  for (let index = 0; index < Object.keys(e).length; index++) {
    var ObjIndex = Object.keys(e)[index]
    PicList[ObjIndex] = baseUrl + e[ObjIndex]
  }
  return PicList
}
